﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Bestlife.ERP.DataAccess;

public partial class Web_Test : System.Web.UI.Page
{
    int count = 0;
    string st = "";
    int agent = 0;
    int ruby = 0;
    int diamand = 0;
    int SDiamand = 0;
    int SEDiamand = 0;
    int CDiamand = 0;
    Boolean check=false;

    List<string> childlist = new List<string>();//monthly st_process
    List<string> childlistnew = new List<string>();//monthly st_process

    List<string> dislist = new List<string>();//monthly Lship Process
    List<string> subchild = new List<string>();
    List<int> returnst = new List<int>();
    List<List<int>> insidelist = new List<List<int>>();

        
    Bestlife.ERP.BusinessRules.rubyTocrown rb_to_crown = new Bestlife.ERP.BusinessRules.rubyTocrown();
    Bestlife.ERP.DataAccess.D_child dch = new Bestlife.ERP.DataAccess.D_child();
    Bestlife.ERP.Entities.E_Level_Genarator lvlgen = new Bestlife.ERP.Entities.E_Level_Genarator();
    Bestlife.ERP.BusinessRules.root_level rl = new Bestlife.ERP.BusinessRules.root_level();
    Bestlife.ERP.BusinessRules._5_Agent fivetoagent = new Bestlife.ERP.BusinessRules._5_Agent();
    Bestlife.ERP.DataAccess.D_bonus getchild = new Bestlife.ERP.DataAccess.D_bonus();
    Bestlife.ERP.Entities.E_Bonus ebns = new Bestlife.ERP.Entities.E_Bonus();//monthly st_process
   
    Bestlife.ERP.BusinessRules.inserts ins = new Bestlife.ERP.BusinessRules.inserts();
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Image1.Visible = false;
            //Bestlife.ERP.BusinessRules.Monthly_Process mp = new Bestlife.ERP.BusinessRules.Monthly_Process();
            //mp.monthcheck();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Image1.Visible = true;
        int rootlevel = int.Parse(rl.root_levelID());

        for (int y = rootlevel; y > 0; y--)
        {

            int level = y;


            childlist = rl.Get_root_levelID(level.ToString());
            //Timer1.Enabled = false;
            foreach (string child_id in childlist)
            {
                insidelist.Clear();
                //get cpv n ppv
                

                Bestlife.ERP.DataAccess.DistributerAC dis = new Bestlife.ERP.DataAccess.DistributerAC();
                ebns = getchild.Getbonus(child_id);

                //.tpv
                double Tpv = double.Parse(ebns.Tpv.ToString());

                //cpv
                double cpv = double.Parse(ebns.Cpv.ToString());
                //ppv
                double ppv = double.Parse(ebns.Ppv.ToString());

                lvlgen = ins.return_Dis_Level(child_id);//get dis entity in tabledislevel
                //find User level and update 
             
                string Dplanst = dis.Dplan(child_id);
                #region 5% to Agent ////chnge by chathuranga five_to_5_Agent return boolean only
                if (!Dplanst.Equals("I"))
                {
                    Boolean bb = fivetoagent.five_to_5_Agent(child_id, cpv, ppv, lvlgen.Level);//get st 5 to agent 
                    Label1.Text = st;
                }
                else
                {

                    if (cpv >= 300 && cpv < 1500 && ebns.User_Status.Equals("5.1"))
                    {

                        try
                        {
                            Boolean upd = false;
                            string Rank = "1";
                            String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                            Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                            String updStatus = "UPDATE Bonus  SET user_Status='5' WHERE User_id='" + child_id + "'";
                            upd = Bestlife.ERP.DataAccess.clsCon.AddEditDel(updStatus);
                            Rank = "5";
                            String Agent = "INSERT INTO Agent (User_id,level_id,Agent_status)VALUES('" + child_id + "','" + lvlgen.Level + "','" + Rank + "')";
                            Boolean chk = Bestlife.ERP.DataAccess.clsCon.AddEditDel(Agent);

                        }


                        catch
                        {

                        }


                        finally
                        {
                            clsCon.conn_new.Close();

                        }
                    }
                    //Boolean checkinsert=  ins.insert_stat(child_id, lvlgen.Level, st);//insert to tables


                }
                #endregion

                #region check up to agent


                subchild = dch.childs(child_id);   //get sub childs 

                if (subchild.Count >= 0)//change by chathuranga 04-08-2012
                {
                    Bestlife.ERP.Entities.E_Level_Genarator elvl = new Bestlife.ERP.Entities.E_Level_Genarator();

                 

                    foreach (string subch in subchild)//Ex -DisId  1->2 n ->3
                    {

                        //get levelid using subid
                        elvl = ins.return_Dis_Level(subch);
                        string lvlid = elvl.Level;
                        string Lvl_status = ebns.User_Status;//get chaild parent status
                       
                        //pass level id and parent status ==> Findst(); to get agent-Cdiamand St
                        returnst = ins.Findandreturn_St(lvlid, Lvl_status);//childs all st agent tocrown diamand
                        insidelist.Add(returnst);

                        #region check cfommented by chathuranga -024-09-2012
                        //int timecount = 0;
                        //foreach (int lire in returnst)
                        //{
                        //    if (timecount == 0)
                        //    {
                        //        if (lire > 0)
                        //        {
                        //            // if (agent>=3)
                        //            //{
                        //            Num_agents++;
                        //            //}
                        //        }

                        //    }
                        //    else if (timecount == 1)
                        //    {
                        //        if (lire > 0)
                        //        {
                        //            Num_Ruby++;
                        //        }
                        //    }
                        //    else if (timecount == 2)
                        //    {
                        //        if (lire > 0)
                        //        {
                        //            Num_diamond++;
                        //        }

                        //    }
                        //    else if (timecount == 3)
                        //    {
                        //        if (lire > 0)
                        //        {
                        //            Num_SD++;
                        //        }

                        //    }
                        //    else if (timecount == 4)
                        //    {
                        //        if (lire > 0)
                        //        {
                        //            Num_SED++;
                        //        }
                        //    }
                        //    else if (timecount == 5)
                        //    {
                        //        if (lire > 0)
                        //        {
                        //            Num_CD++;
                        //        }
                        //    }


                        //    timecount++;
                        //}
                        #endregion

                        //Boolean rt = rb_to_crown.returntoBrake(Num_agents, Num_Ruby, Num_diamond, Num_SD, Num_SED, Num_CD, ebns.User_Status);

                        //if (rt)
                        //{
                        //    break;
                        //}
                    }

                    //cll check Rb to crown
                    rb_to_crown.checkRb_to_Crown(child_id, Tpv, ppv, cpv, insidelist, lvlgen.Level);


                }

                subchild.Clear();//have to check 

                #endregion

               

            }
            childlist.Clear();
        }
    }
            //Image1.Visible = false;

    protected void Button2_Click(object sender, EventArgs e)
    {
        
        #region declaration and variables
        
        Bestlife.ERP.DataAccess.D_bonus getDetails = new Bestlife.ERP.DataAccess.D_bonus();
        Bestlife.ERP.DataAccess.parents parent = new Bestlife.ERP.DataAccess.parents();
        Bestlife.ERP.Entities.E_Bonus ebnschild = new Bestlife.ERP.Entities.E_Bonus();//monthly Lship Process
        Bestlife.ERP.Entities.E_Bonus ebnsparent = new Bestlife.ERP.Entities.E_Bonus();//monthly Lship Process
        #endregion

        #region monthly Lship St
        //pass parent st n parent
        int rootlevel = int.Parse(rl.root_levelID());
        Bestlife.ERP.BusinessRules.monthLshipst lshipst = new Bestlife.ERP.BusinessRules.monthLshipst();
        for (int y = rootlevel; y > 0; y--)
        {

            int level = y;
            dislist = rl.Get_root_levelID(level.ToString());//get childs using parent_to_user


            foreach (string disIDs in dislist)
            {
                ebnschild = getDetails.Getbonus(disIDs);//to get child st

                String Parent_1 = parent.parent(disIDs);
                ebnsparent = getDetails.Getbonus(Parent_1);// to get parent st

                //have to check if distributor purches any item or not
                if ((!(Parent_1 == null) && !(ebnschild.User_Status == null)))
                {
                    if (ebnsparent.User_Status.ToString().Equals(ebnschild.User_Status.ToString()))
                    {

                        decimal LshipPV = lshipst.returnLshipCPV(disIDs);
                        lshipst.BonusLship_Update(disIDs, LshipPV);//update parent(get parent in BonusLship_Update--> ok )

                    }
                }
            }
            
            dislist.Clear();
            
        }

        
        #endregion 
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
       
        #region declaration and variables

        Decimal TotSCommision = 0;
        Decimal TotCommisiontoparent = 0;
        List<string> Listby_Level = new List<string>();

        Bestlife.ERP.DataAccess.parents pp = new Bestlife.ERP.DataAccess.parents();
        Bestlife.ERP.Entities.E_Bonus E_chlist = new Bestlife.ERP.Entities.E_Bonus();
        Bestlife.ERP.DataAccess.D_bonus getData = new Bestlife.ERP.DataAccess.D_bonus();
        Bestlife.ERP.Entities.E_Bonus E_getparent = new Bestlife.ERP.Entities.E_Bonus();

        #endregion

        int rootlevel = int.Parse(rl.root_levelID());

        for (int y = rootlevel; y > 0; y--)
        {
            Listby_Level = rl.Get_root_levelID(y.ToString());
            foreach (string Chlist in Listby_Level)
            {
                E_chlist = getData.Getbonus(Chlist);

                int userst = int.Parse(E_chlist.User_Status);
                if ((userst <= 6 && (E_chlist.Ppv < 25)) || (userst == 7 && (E_chlist.Ppv < 50)) || (userst == 8 && (E_chlist.Ppv < 75)) || (userst == 9 && (E_chlist.Ppv < 100)) || (userst == 10 && (E_chlist.Ppv < 200))) //if Person Who dosent Reach his monthly process availability
                {
                    #region Update process

                    TotSCommision = decimal.Parse(E_chlist.Storecomm.ToString());

                    string Listby_Returns = pp.parentAllRunner(Chlist, E_chlist.User_Status);//Return Parent Who Accept to get child Commission

                    E_getparent = getData.Getbonus(Listby_Returns);//Get parent Details
                    TotCommisiontoparent = E_getparent.T_commision + TotSCommision;

                    bool ReturnSt = getData.UpdatemonthlyUp(Listby_Returns, TotCommisiontoparent);//update parent commision

                    #endregion
                }

            }

            Listby_Level.Clear();
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Bestlife.ERP.Entities.E_Bonus E_chlist = new Bestlife.ERP.Entities.E_Bonus();
        Bestlife.ERP.DataAccess.D_bonus getData = new Bestlife.ERP.DataAccess.D_bonus();
        Bestlife.ERP.DataAccess.parents p1 = new Bestlife.ERP.DataAccess.parents();
        Bestlife.ERP.Entities.E_Bonus ebns = new Bestlife.ERP.Entities.E_Bonus();
        Bestlife.ERP.DataAccess.D_bonus bns = new Bestlife.ERP.DataAccess.D_bonus();

        List<string> Listby_Level = new List<string>();
        int rootlevel = int.Parse(rl.root_levelID());
        
       
        for (int y = rootlevel; y > 1; y--)
        {
          //  level.Text = y.ToString();
            Listby_Level.Clear();

            Listby_Level = rl.Get_root_levelID(y.ToString());
            foreach (string userid in Listby_Level)
            {
            
            string parent = p1.parent(userid);//Find Parent
            ebns = bns.Getbonus(parent);

            E_chlist= getData.Getbonus(userid);
            string user_status = E_chlist.User_Status.ToString();
            int userst = int.Parse(user_status);
            double tpv_month=double.Parse(E_chlist.Tppv_month.ToString());
            double tbv_month=double.Parse(E_chlist.Tbv_month.ToString());
            string userStstring = user_status.ToString();
           
            if (userst > 6)
                { 
                  if(user_status.Equals("7") && (ebns.User_Status.Equals("7")))
                  {
                      Bestlife.ERP.BusinessRules.leadership ld1 = new Bestlife.ERP.BusinessRules.leadership();
                      String get_returnID = ld1.calculate_Lbonus(userid, userStstring, tpv_month, tbv_month);
                  }
                  else if (user_status.Equals("8") && (ebns.User_Status.Equals("8")))
                  {
                      Bestlife.ERP.BusinessRules.leadership ld1 = new Bestlife.ERP.BusinessRules.leadership();
                      String get_returnID = ld1.calculate_Lbonus(userid, userStstring, tpv_month, tbv_month);
                  }
                  else if (user_status.Equals("9") && (ebns.User_Status.Equals("9")))
                  {
                      Bestlife.ERP.BusinessRules.leadership ld1 = new Bestlife.ERP.BusinessRules.leadership();
                      String get_returnID = ld1.calculate_Lbonus(userid, userStstring, tpv_month, tbv_month);
                  }
                  else if (user_status.Equals("10") && (ebns.User_Status.Equals("10")))
                  {
                      Bestlife.ERP.BusinessRules.leadership ld1 = new Bestlife.ERP.BusinessRules.leadership();
                      String get_returnID = ld1.calculate_Lbonus(userid, userStstring, tpv_month, tbv_month);
                  }
    
                }
            }
        }
    }

    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        //String path = Request.Url.AbsolutePath;
        //// String Status= "true";
        //String gid = this.GridView1.SelectedRow.Cells[1].Text;

        //Application["id"] = gid;
        //Application["path"] = path;
        ////  Response.Redirect("Registration.aspx");
    }


    //protected void ProgressBar1_RunTask(object sender, EO.Web.ProgressTaskEventArgs e)
    //{
        
    //    e.UpdateProgress(0,"start");

        
    //    for (int i = 0; i < 100; i++)
    //    {
            
    //        if (e.IsStopped)
    //            break;

           
    //        System.Threading.Thread.Sleep(500);

    //        e.UpdateProgress(i);
    //    }

    //    e.UpdateProgress(100, "Complete");
        
        
    //}


    protected void Button5_Click(object sender, EventArgs e)
    {
        #region new st process

         int rootlevel = int.Parse(rl.root_levelID());

         for (int y = rootlevel; y > 0; y--)
         {
                int level = y;//get low level person

                childlistnew = rl.Get_root_levelID(level.ToString());//gest last level distributors set

                foreach (string chilID in childlistnew) //One level child set <Ex- parant to user level 3 >
                {

                    Bestlife.ERP.DataAccess.D_bonus dbnsnew = new D_bonus();
                    Bestlife.ERP.Entities.E_Bonus ebnsnew = new Bestlife.ERP.Entities.E_Bonus();
                    Bestlife.ERP.BusinessRules.inserts insnew = new Bestlife.ERP.BusinessRules.inserts();
                    Bestlife.ERP.Entities.E_Level_Genarator lvlgennew = new Bestlife.ERP.Entities.E_Level_Genarator();


                    ebnsnew = dbnsnew.Getbonus(chilID);
                    lvlgennew = insnew.return_Dis_Level(chilID);

                    #region 5% To Agent

                    Bestlife.ERP.BusinessRules._5_Agent fivetoagent=new Bestlife.ERP.BusinessRules._5_Agent();
                    Boolean five_to_Agnt= fivetoagent.five_to_5_Agent(chilID, double.Parse(ebnsnew.Ppv.ToString()), double.Parse(ebnsnew.Ppv.ToString()),lvlgennew.Level);

                    #endregion

                    #region Agent To up


                    #endregion


                }
         }


        #endregion
    }
}